package com.neu.admin;

import com.neu.doc.*;
import com.neu.domain.ShoppingAd;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.neu.common.RequiresPermissionsDesc;
import com.neu.common.validator.Order;
import com.neu.common.validator.Sort;
import com.neu.domain.ShoppingGoods;
import com.neu.domain.ShoppingTopic;
import com.neu.service.ShoppingGoodsService;
import com.neu.service.ShoppingTopicService;
import com.neu.util.ResponseUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/topic")
@Validated
public class AdminTopicController {
    private final Log logger = LogFactory.getLog(AdminTopicController.class);

    @Autowired
    private ShoppingTopicService topicService;
    @Autowired
    private ShoppingGoodsService goodsService;


    @ApiOperation(value="获取专题管理分页数据", notes="分页查询专题管理",response = AdminTopicController_list.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "每页多少条数据", required = true, dataType = "int"),
            @ApiImplicitParam(name = "sort", value = "排序字段", required = true, dataType = "string"),
            @ApiImplicitParam(name = "order", value = "排序规则", required = true, dataType = "string"),
            @ApiImplicitParam(name = "title", value = "专题标题", required = false, dataType = "string"),
            @ApiImplicitParam(name = "subtitle", value = "专题sub标题", required = false, dataType = "string")
    })
    @RequiresPermissions("admin:topic:list")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String title, String subtitle,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order){
        List<ShoppingTopic> topics=topicService.querySelective(title,subtitle,page,limit,sort,order);
        return ResponseUtil.okList(topics);

    }
   

    private Object validate(ShoppingTopic topic) {
        String title = topic.getTitle();
        if (StringUtils.isEmpty(title)) {
            return ResponseUtil.badArgument();
        }
        String content = topic.getContent();
        if (StringUtils.isEmpty(content)) {
            return ResponseUtil.badArgument();
        }
        BigDecimal price = topic.getPrice();
        if (price == null) {
            return ResponseUtil.badArgument();
        }
        return null;
    }


    @ApiOperation(value="添加专题管理", notes="添加一条专题管理", response = AdminTopicController_ResOK.class)
    @RequiresPermissions("admin:topic:create")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)@RequestBody ShoppingTopic topic){
        Object error = validate(topic);
        if (error != null){
            return error;
        }
        topicService.add(topic);
        return   ResponseUtil.ok(topic);
    }

//    @ApiIgnore//使用该注解忽略这个API
    @RequiresPermissions("admin:topic:read")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        ShoppingTopic topic = topicService.findById(id);
        Integer[] goodsIds = topic.getGoods();
        List<ShoppingGoods> goodsList = null;
        if (goodsIds == null || goodsIds.length == 0) {
            goodsList = new ArrayList<>();
        } else {
            goodsList = goodsService.queryByIds(goodsIds);
        }
        Map<String, Object> data = new HashMap<>(2);
        data.put("topic", topic);
        data.put("goodsList", goodsList);
        return ResponseUtil.ok(data);
    }

    @ApiOperation(value="编辑专题管理", notes="编辑一条专题管理", response = AdminTopicController_ResOK.class)
    @RequiresPermissions("admin:topic:update")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
                             @RequestBody ShoppingTopic topic){
        Object error = validate(topic);
        if (error != null) {
            return error;
        }
        if (topicService.updateById(topic) == 0) {
            return ResponseUtil.updatedDataFailed();
        }
        topicService.updateById(topic);
        return ResponseUtil.ok(topic);
    }



    @ApiOperation(value="删除专题管理", notes="删除一条专题管理", response = ResponseOK.class)
    @RequiresPermissions("admin:topic:delete")
    @RequiresPermissionsDesc(menu = {"推广管理", "专题管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@ApiParam(name="ShoppingAd對象",value="传入json格式",required=true)
                        @RequestBody ShoppingTopic topic) {
        Integer id = topic.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        topicService.deleteById(id);
        return ResponseUtil.ok();
    }

}


